﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Web.Controllers
{
    public class TemplateController : CustomBaseController
    {
        //
        // GET: /Template/
        [CustomAuthorise(Flag = AuthoriseFlag.page_sms_template)]
        public ActionResult Index(TemplateModel model)
        {
            if (model.page == 0)
                model.page = 1;
            if (model.key == null)
                model.key = "";
            model.IsChangeUrl = true;
            WebConfigProvider csp = new WebConfigProvider();
            int max = Convert.ToInt32(csp.MaxshowInHistory);
            ITemplateService ts = new TemplateDataAccess(csp);
            //得到当前用户
            IUserService us = new UserService(csp);
            int userid = us.GetUserItemByCIL(User.Identity.Name).UserID;
            model.list = ts.GetTemplatesByUserID(userid, model.page, max, model.key);
            model.maxpages = ts.GetPageCount(userid, max, model.key);
            return View(model);
        }

        //[HttpPost]
        [CustomAuthorise(Flag = AuthoriseFlag.page_sms_template)]
        public ViewResult DeleteTemplate(int id, TemplateModel model)
        {
            WebConfigProvider csp = new WebConfigProvider();
            ITemplateService ts = new TemplateDataAccess(csp);
            ts.DeleteTemplateByID(id);

           // TemplateModel model = new TemplateModel();
            if (model.page == 0)
                model.page = 1;
            if (model.key == null)
                model.key = "";
            model.IsChangeUrl = false;

            int max = Convert.ToInt32(csp.MaxshowInHistory);
            //得到当前用户
            IUserService us = new UserService(csp);
            int userid = us.GetUserItemByCIL(User.Identity.Name).UserID;
            model.list = ts.GetTemplatesByUserID(userid, model.page, max, model.key);
            model.maxpages = ts.GetPageCount(userid, max, model.key);
            return View("TemplateGrid", model);
        }

         
        [CustomAuthorise(Flag = AuthoriseFlag.page_sms_template)]
        public ViewResult DeleteTemplates(string[] items, TemplateModel model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            ITemplateService dataAccess = new TemplateDataAccess(configProvider);
            if (items != null && items.Length != 0)
            {
                foreach (var id in items)
                {
                    dataAccess.DeleteTemplateByID(Convert.ToInt32(id));
                }
            }
            //TemplateModel model = new TemplateModel();
            if (model.page == 0)
                model.page = 1;
            if (model.key == null)
                model.key = "";
            model.IsChangeUrl = false;

            int max = Convert.ToInt32(configProvider.MaxshowInHistory);
            //得到当前用户
            IUserService us = new UserService(configProvider);
            int userid = us.GetUserItemByCIL(User.Identity.Name).UserID;
            model.list = dataAccess.GetTemplatesByUserID(userid, model.page, max, model.key);
            model.maxpages = dataAccess.GetPageCount(userid, max, model.key);
            return View("TemplateGrid", model);
        }
        
        [HttpPost]
        [CustomAuthorise(Flag = AuthoriseFlag.page_sms_template)]
        public ActionResult AddTemplates(string content,int id)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            ITemplateService dataAccess = new TemplateDataAccess(configProvider);
            //得到当前用户
            IUserService us = new UserService(configProvider);
            int userid = us.GetUserItemByCIL(User.Identity.Name).UserID;
            if (content.Trim() == "")
            {
                TempData["msg"] = App_GlobalResources.Resources.Controller_Template_Need_Content;
                return Redirect("Index");
            }
            if (id.ToString().Trim().Length != 0 && id != 0)
            {
                dataAccess.UpdateTemplate(id,content.Trim());
                return Redirect("Index");
            }
            else
            {
                dataAccess.CreateNewTemplate(userid,content.Trim());
                return Redirect("Index");
            }
         }
    }
}
